<template>
  <!-- // 分配角色 -->
  <div>
    <el-checkbox-group v-model="checkList">
      <el-checkbox v-for="item in roleIds" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button @click="$emit('close')">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRoleAll } from '@/api/settings'
import { addAssignRole, getProfileById } from '@/api/user'
export default {
  props: {
    roleId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      roleIds: [], // 用来保存获取的权限列表
      checkList: [] // 用来存储多选框选中的值
    }
  },
  created() {
    this.loadRoleAll()
  },
  methods: {
    async hSubmit() {
      // 因为需要的传递的数据有id和roleIds
      const res = await addAssignRole({ id: this.roleId, roleIds: this.checkList })
      console.log(res)
      this.$message.success(res.message)
      this.$emit('close')
    },
    async loadRoleAll() {
      try {
        const res = await getRoleAll()
        // console.log(res, 'loadRoleAll')
        this.roleIds = res.data.rows
        const info = await getProfileById(this.roleId)
        console.log(info)
        this.checkList = info.data.roleIds
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
